package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.injector.methods.DeleteById;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdChannelMapper;
import com.heima.admin.service.AdChannelService;
import com.heima.model.admin.dtos.ChannelDto;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author lenovo
 */
@Service
public class AdChannelServiceImpl extends ServiceImpl<AdChannelMapper, AdChannel> implements AdChannelService {

    Date now=new Date();

    @Override
    public ResponseResult list(ChannelDto channelDto){
//        判空
        if (channelDto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }

//        初始化page
        channelDto.checkParam();

        LambdaQueryWrapper<AdChannel> lambdaQueryWrapper=new LambdaQueryWrapper<>();
//        频道名拼接
        if (StringUtils.isNotBlank(channelDto.getName())){
            lambdaQueryWrapper.like(AdChannel::getName,channelDto.getName());
        }

        IPage<AdChannel> iPage=new Page<>(channelDto.getPage(),channelDto.getSize());
        page(iPage,lambdaQueryWrapper);

        PageResponseResult responseResult=new PageResponseResult(channelDto.getPage(),channelDto.getSize(),(int) iPage.getTotal());
        responseResult.setData(iPage.getRecords());
        return responseResult;
    }

    @Override
    public ResponseResult insert(AdChannel adChannel) {
//        判参
        if (adChannel==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }

        adChannel.setCreatedTime(now);
        adChannel.setIsDefault(true);

//        添加
        save(adChannel);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult update(AdChannel adChannel) {
//        判参
        if (adChannel==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }

        AdChannel adChannelBj=new AdChannel();
        if (getById(adChannel.getId())==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        updateById(adChannel);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult delete(Integer id) {
        if (id==null||id==0){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }

        if (getById(id)==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        if (getById(id).getStatus()){
            return ResponseResult.errorResult(AppHttpCodeEnum.LOGIC_ERROR,"数据启用不能删除");
        }

        removeById(id);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
